package com.alageek.study.controller;

import com.alageek.study.dao.BookDao;
import com.alageek.study.entity.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class BookController {

    @Autowired
    BookDao bookDao;
    @Autowired
    MongoTemplate mongoTemplate;

    @GetMapping("/test1")
    public void test1(){
        List<Book> books = new ArrayList<>();
        Book b1 = new Book(1,"朝花夕拾","鲁迅");
        Book b2 = new Book(2,"呐喊","鲁迅");
        books.add(b1);
        books.add(b2);
        bookDao.insert(books);
        List<Book> books1 = bookDao.findByAuthorContains("鲁迅");
        Book book = bookDao.findByNameEquals("朝花夕拾");
        System.out.println(books1);
        System.out.println(book);
    }

    @GetMapping("/test2")
    public void test2(){
        List<Book> books = new ArrayList<>();
        Book b1 = new Book(3,"围城","钱钟书");
        Book b2 = new Book(4,"宋诗选注","钱钟书");
        books.add(b1);
        books.add(b2);
        mongoTemplate.insertAll(books);
        List<Book> list = mongoTemplate.findAll(Book.class);
        Book book = mongoTemplate.findById(3, Book.class);
        System.out.println(list);
        System.out.println(book);
    }

}
